package com.ccl.iot.dialogs;

import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.ccl.iot.views.ModifyStyleItemView;
import com.ccl.iot.R;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.device.IOTDeviceConst;

/**
 * 修改单个设备图标的dialog
 */
public class ModifyIconDialog extends EditDialog {
	
	private IOTDevice mDevice;
	private Context mContext;
	
	private int oldId = 0;
	
	private ModifyStyleItemView mItems;
	
	//此标记表示是否将修改同步到服务器
	private boolean isSync = false;
	
	private int mIconId;
	
	private OnSelectChangeListener mListener;
	public interface OnSelectChangeListener{
		public void onSelectChange();
	}

	public ModifyIconDialog(Context context, IOTDevice dev, OnSelectChangeListener listener) {
		super(context);
		mContext = context;
		mDevice = dev;
		mListener = listener;
	}

	@Override
	protected boolean onOK() {
		//当点击了保存则将修改同步到服务器
		isSync = true;
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(mDevice.getName());
		
		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		
		mItems = new ModifyStyleItemView(mContext);
		
		this.setContent(mItems, lp);
		initView();
	}
	
	private void initView(){
		if(mDevice != null){
			//根据设备的类型显示相应的图标
			if((mDevice.getType() & IOTDeviceConst.TDEVICE_TYPE.EDEVICE_TYPE_TERMINAL) == IOTDeviceConst.TDEVICE_TYPE.EDEVICE_TYPE_TERMINAL){
				selectSwitchIcon();
			}else if((mDevice.getType() & IOTDeviceConst.TDEVICE_TYPE.EDEVICE_TYPE_TERMINAL_DOOR) == IOTDeviceConst.TDEVICE_TYPE.EDEVICE_TYPE_TERMINAL_DOOR){
				selectDoorIcon();
			}
		}
	}

	/**
	 * 选择显示门窗的图标
	 */
	private void selectDoorIcon() {
		if(mItems != null){
			mItems.showDoorIcon(mDevice.getSplitIconId());
			selectChange();
		}
	}

	/**
	 * 选择显示灯的图标
	 */
	private void selectSwitchIcon() {
		if(mItems != null){
			mItems.showSwitchIcon(mDevice.getSplitIconId());
			selectChange();
		}
	}
	
	private void selectChange(){
		mItems.setOnIconItemChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int tmp = 0;
				switch (checkedId) {
				case R.id.radio0:
					tmp = DeviceIconProvider.ICON_ID.FIRST_ICON;
					break;
				case R.id.radio1:
					tmp = DeviceIconProvider.ICON_ID.SECOND_ICON;
					break;
				case R.id.radio2:
					tmp = DeviceIconProvider.ICON_ID.THREE_ICON;
					break;
				case R.id.radio3:
					tmp = DeviceIconProvider.ICON_ID.FOUR_ICON;
					break;
				case R.id.radio4:
					tmp = DeviceIconProvider.ICON_ID.FIVE_ICON;
					break;
				case R.id.radio5:
					tmp = DeviceIconProvider.ICON_ID.SIX_ICON;
					break;
				}
				
				mIconId = mDevice.getNoSplitIconId();
				mIconId = mIconId & 0xff000000;
				mIconId = mIconId | tmp;
				//修改图标的id
				mDevice.setIconId(mIconId, isSync);
				
				if(mListener != null){
					//不管修改是否同步到服务器,都要刷新页面讲修改的图标显示到页面上
					mListener.onSelectChange();
				}
			}
		});
	}
	
	@Override
	protected void onShow() {
		super.onShow();
		if(mDevice != null){
			//将图标原来的id记录下来,如果用户最后选择取消修改,则将设备的图标还原
			oldId = mDevice.getNoSplitIconId();
		}
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		if(mDevice != null){
			//最后根据标记来判断是否同步到服务器
			if(isSync){
				mDevice.setIconId(mIconId, isSync);
				isSync = false;
			}else{
				mDevice.setIconId(oldId, isSync);
				mDevice.resetIconId();
			}
		}
		if(mListener != null){
			mListener.onSelectChange();
		}
	}
	
}
